#include <config.h>
#include <debug.h>
#include <header.h>
#include <rules.h>
#include <xml_util.h>

#include "gui.h"
#include "language.h"

GtkWidget *main_window = NULL;
static GtkWidget *get_menu_bar();
static GtkWidget *get_notebook(void);

void show_main_window(void)
{
  GtkWidget *vbox = NULL;

  GtkWidget *menubar = NULL;
  GtkWidget *hbox = NULL;

  GtkWidget *button_vbox = NULL;
  GtkWidget *notebook_vbox = NULL;

  GtkWidget *button = NULL;
  GtkWidget *notebook = NULL;

  if (main_window)
  {
    if (GTK_WIDGET_VISIBLE(main_window))
    {
      gtk_widget_hide(GTK_WIDGET(main_window));
    }

    gtk_widget_show(GTK_WIDGET(main_window));

    return;
  }

  main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  {
    vbox = gtk_vbox_new(FALSE, 10);
    gtk_container_add(GTK_CONTAINER(main_window), vbox);
    {
      menubar = get_menu_bar();
      gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);

      hbox = gtk_hbox_new(FALSE, 0);
      gtk_container_border_width(GTK_CONTAINER(hbox), 0);
      gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
      {
        button_vbox = gtk_vbutton_box_new();
        /* Set the appearance of the Button Box */
        gtk_button_box_set_layout(GTK_BUTTON_BOX (button_vbox),
                                  GTK_BUTTONBOX_SPREAD);
        gtk_box_set_spacing(GTK_BOX (button_vbox), 20);

        gtk_box_pack_start(GTK_BOX(hbox), button_vbox, FALSE, FALSE, 5);
        {
          button = gtk_button_new();
          gtk_container_add(GTK_CONTAINER(button),
                            gtk_image_new_from_file("icon_start_normal.png"));
          gtk_button_set_relief(GTK_BUTTON (button), GTK_RELIEF_NONE);
          GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
          gtk_box_pack_start(GTK_BOX(button_vbox), button, FALSE, FALSE, 5);

          button = gtk_button_new();
          gtk_container_add(GTK_CONTAINER(button),
                            gtk_image_new_from_file("icon_start_normal.png")); 
          gtk_button_set_relief(GTK_BUTTON (button), GTK_RELIEF_NONE);
          GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
          gtk_box_pack_start(GTK_BOX(button_vbox), button, FALSE, FALSE, 5);

          button = gtk_button_new();
          gtk_container_add(GTK_CONTAINER(button),
                            gtk_image_new_from_file("icon_start_normal.png")); 
          gtk_button_set_relief(GTK_BUTTON (button), GTK_RELIEF_NONE);
          GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
          gtk_box_pack_start(GTK_BOX(button_vbox), button, FALSE, FALSE, 5);
        }

        notebook_vbox = gtk_vbox_new(FALSE, 0);
        gtk_container_border_width(GTK_CONTAINER(notebook_vbox), 0);
        gtk_box_pack_start(GTK_BOX(hbox), notebook_vbox, FALSE, FALSE, 0);
        {
          notebook = get_notebook();
          gtk_box_pack_start(GTK_BOX(notebook_vbox), notebook, FALSE, FALSE, 0);
        }
      }
    }
  }

  gtk_window_set_icon_from_file(GTK_WINDOW(main_window),
                                COOLNET_TRAY_ICON, NULL);
  gtk_window_set_default_size(GTK_WINDOW(main_window),
                              MAIN_WINDOW_WIDTH ,
                              MAIN_WINDOW_HEIGHT);
  /*connect signal*/
  g_signal_connect(G_OBJECT(main_window), "delete_event",
                   G_CALLBACK(gtk_widget_hide_on_delete), NULL);
  gtk_widget_show_all(GTK_WIDGET(main_window));
}

static GtkWidget *get_menu_bar()
{
  GtkWidget *menubar = NULL;

  GtkWidget *menu_item_op = NULL;
  GtkWidget *menu_item_set = NULL;
  GtkWidget *menu_item_help = NULL;

  GtkWidget *sub_menu_op = NULL;
  GtkWidget *sub_menu_set = NULL;
  GtkWidget *sub_menu_help = NULL;

  GtkWidget *sub_menu_item_start = NULL;
  GtkWidget *sub_menu_item_exit = NULL;
  GtkWidget *sub_menu_item_disconnect = NULL;


  GtkWidget *sub_menu_item_app = NULL;
  GtkWidget *sub_menu_item_custom = NULL;
  GtkWidget *sub_menu_item_advance = NULL;
  GtkWidget *sub_menu_item_netstat = NULL;
  GtkWidget *sub_menu_item_preference = NULL;


  GtkWidget *sub_menu_item_about = NULL;

  menubar = gtk_menu_bar_new();
  {
    menu_item_op = gtk_menu_item_new_with_label(TEXT_MENU_OPERATION);
    gtk_menu_bar_append(GTK_MENU_BAR(menubar), menu_item_op);
    {
      sub_menu_op = gtk_menu_new();
      gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_op), sub_menu_op);
      {
        sub_menu_item_start = gtk_menu_item_new_with_label(TEXT_SUB_MENU_START);
        gtk_menu_append(GTK_MENU(sub_menu_op), sub_menu_item_start);

        sub_menu_item_exit = gtk_menu_item_new_with_label(TEXT_SUB_MENU_DISCONNECT);
        gtk_menu_append(GTK_MENU(sub_menu_op), sub_menu_item_exit);

        sub_menu_item_disconnect = gtk_menu_item_new_with_label(TEXT_SUB_MENU_EXIT);
        gtk_menu_append(GTK_MENU(sub_menu_op), sub_menu_item_disconnect);
      }
    }

    menu_item_set = gtk_menu_item_new_with_label(TEXT_MENU_SET);
    gtk_menu_bar_append(GTK_MENU_BAR(menubar), menu_item_set);
    {
      sub_menu_set = gtk_menu_new();
      gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_set), sub_menu_set);
      {
        sub_menu_item_app = gtk_menu_item_new_with_label(TEXT_SUB_MENU_APP);
        gtk_menu_append(GTK_MENU(sub_menu_set), sub_menu_item_app);

        sub_menu_item_custom = gtk_menu_item_new_with_label(TEXT_SUB_MENU_CUSTOM);
        gtk_menu_append(GTK_MENU(sub_menu_set), sub_menu_item_custom);

        sub_menu_item_advance = gtk_menu_item_new_with_label(TEXT_SUB_MENU_ADVANCE);
        gtk_menu_append(GTK_MENU(sub_menu_set), sub_menu_item_advance);

        sub_menu_item_netstat = gtk_menu_item_new_with_label(TEXT_SUB_MENU_NETSTAT);
        gtk_menu_append(GTK_MENU(sub_menu_set), sub_menu_item_netstat);

        sub_menu_item_preference = gtk_menu_item_new_with_label(TEXT_SUB_MENU_PREFERENCE);
        gtk_menu_append(GTK_MENU(sub_menu_set), sub_menu_item_preference);
      }
    }

    menu_item_help = gtk_menu_item_new_with_label(TEXT_MENU_HELP);
    gtk_menu_bar_append(GTK_MENU_BAR(menubar), menu_item_help);
    {
      sub_menu_help = gtk_menu_new();
      gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_help), sub_menu_help);
      {
        sub_menu_item_about = gtk_menu_item_new_with_label(TEXT_SUB_MENU_ABOUT);
        gtk_menu_append(GTK_MENU(sub_menu_help), sub_menu_item_about);
      }
    }
  }

  return menubar;
}

static GtkWidget *get_notebook(void)
{
  GtkWidget *notebook = NULL;

  GtkWidget *app_page = NULL;
  GtkWidget *app_label = NULL;

  GtkWidget *custom_page = NULL;
  GtkWidget *custom_label = NULL;

  GtkWidget *advance_page = NULL;
  GtkWidget *advance_label = NULL;

  GtkWidget *netstat_page = NULL;
  GtkWidget *netstat_label = NULL;

  GtkWidget *preference_page = NULL;
  GtkWidget *preference_label = NULL;

  notebook = gtk_notebook_new();
  gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
  {
    app_page = get_app_page();
    gtk_container_border_width(GTK_CONTAINER(app_page), 0);
    app_label = gtk_label_new(TEXT_APP);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
                             GTK_WIDGET(app_page), GTK_WIDGET(app_label));

    custom_page = get_custom_page();
    gtk_container_border_width(GTK_CONTAINER(custom_page), 0);
    custom_label = gtk_label_new(TEXT_CUSTOM);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
                             GTK_WIDGET(custom_page), GTK_WIDGET(custom_label));

    advance_page = get_advance_page();
    gtk_container_border_width(GTK_CONTAINER(advance_page), 0);
    advance_label = gtk_label_new(TEXT_ADVANCE);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
                             GTK_WIDGET(advance_page), GTK_WIDGET(advance_label));

    netstat_page = get_netstat_page();
    gtk_container_border_width(GTK_CONTAINER(netstat_page), 0);
    netstat_label = gtk_label_new(TEXT_NETSTAT);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
                             GTK_WIDGET(netstat_page), GTK_WIDGET(netstat_label));

    preference_page = get_preference_page();
    gtk_container_border_width(GTK_CONTAINER(preference_page), 0);
    preference_label = gtk_label_new(TEXT_PREFERENCE);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
                             GTK_WIDGET(preference_page), GTK_WIDGET(preference_label));
  }

  return notebook;
}
